package com.cjl.multi_thread.share_cooperate.lock;

import com.cjl.multi_thread.aqs.Mylock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class EmpressLockCondition {
    private final Lock siteLock = new ReentrantLock();
    private final Condition siteCondition = siteLock.newCondition();

    private final Lock kmLock = new ReentrantLock();
    private final Condition kmCondition = kmLock.newCondition();

    private String site = "Beijing";
    private int km = 100;

    public void changeSite(){
        siteLock.lock();
        try {
            this.site = "Shanghai";
            siteCondition.signal();
        }finally {
            siteLock.unlock();
        }
    }

    public void changeKm(){
        kmLock.lock();
        try {
            this.km = 1000;
            kmCondition.signal();
        }finally {
            kmLock.unlock();
        }
    }

    public void checkSite(){
        siteLock.lock();
        try {
            while ("Beijing".equals(this.site)){
                siteCondition.await();
            }
            System.out.println("change Site");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            siteLock.unlock();
        }
    }

    public void checkKm(){
        kmLock.lock();
        try {
            while (this.km == 100){
                kmCondition.await();
            }
            System.out.println("change Km");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            kmLock.unlock();
        }
    }
}
